package controller

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntityManager"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/httpserver"
	"go-fox-edge-kernel-manager/utils/PageUtils"
	"strings"
)

type channelStatusManageController struct {
}

func (e *channelStatusManageController) init() {
	httpserver.Server.POST("/channel/status/entities", e.selectEntityList)
	httpserver.Server.POST("/channel/status/page", e.selectPageList)
}

func (e *channelStatusManageController) selectEntityList(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	return e.selectList(context, body, false)
}

func (e *channelStatusManageController) selectPageList(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	return e.selectList(context, body, true)
}

func (e *channelStatusManageController) selectList(context httpserver.Context, body map[string]interface{}, isPage bool) (interface{}, error) {
	entityList := commEntityManager.FindEntityList(edgeEntity.Type.ChannelStatusEntity.Type(), func(value commEntity.IEntity) bool {
		entity := value.(*edgeEntity.ChannelStatusEntity)

		result := true

		if Map.Has(body, "channelName") {
			result = result && strings.Count(entity.ChannelName, body["channelName"].(string)) > 0
		}
		if Map.Has(body, "channelType") {
			result = result && entity.ChannelType == body["channelType"]
		}

		return result
	})

	// 获得分页数据
	if isPage {
		return PageUtils.GetPageList(entityList, body), nil
	} else {
		return commEntity.BuildMapList(entityList), nil
	}
}
